﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNADota.ContentPipeline.Terrain;
using System.Diagnostics;

namespace XNADota.GamePlay
{
    public class ObjectModifyProperty
    {
        public ObjectModifyProperty(WCLoader Loader, bool UseOptionalInts, string oldId, string newId)
        {
            ModificationId = Loader.ReadString(4);
            VariableType = Loader.ReadInt32();
            this.UseOptionalInts = UseOptionalInts;
            if (UseOptionalInts)
            {
                UseOptionalInts = true;
                OptionalInt0 = Loader.ReadInt32();
                OptionalInt1 = Loader.ReadInt32();
            }
            switch (VariableType)
            {
                case 0:
                    IntValue = Loader.ReadInt32();
                    break;
                case 1:
                    FloatValue = Loader.ReadFloat();
                    break;
                case 2:
                    FloatValue = Loader.ReadFloat();
                    break;
                case 3:
                    string content = "";
                    while (true)
                    {
                        string extra = Loader.ReadString(1);
                        content = content + extra;
                        if (extra == "")
                        {
                            break;
                        }
                    }
                    StringValue = content;
                    break;
                default:
                    Debug.Assert(false);
                    break;
            }

            while (true)
            {
                ObjectId = Loader.ReadString(4);

                if (ObjectId.CompareTo(oldId) == 0 || ObjectId.CompareTo(newId) == 0)
                {
                    break;
                }
                else
                {
                    Debug.Assert(false);
                }
            }
        }

        public string ModificationId;
        public string ObjectId;
        public int VariableType;
        public int IntValue;
        public float FloatValue;
        public string StringValue;

        public bool UseOptionalInts;
        public int OptionalInt0;
        public int OptionalInt1;
    }

    public class ObjectModifyUnit
    {
        public ObjectModifyUnit(WCLoader Loader, bool UseOptionalInts)
        {
            OriginalObjectId = Loader.ReadString(4);
            ChangedObjectId = Loader.ReadString(4);
            ModifiedCount = Loader.ReadInt32();
            ObjectModifyProperties = new ObjectModifyProperty[ModifiedCount];
            for (int i = 0; i < ModifiedCount; i++)
                ObjectModifyProperties[i] = new ObjectModifyProperty(Loader, UseOptionalInts, OriginalObjectId, ChangedObjectId);
        }

        public string OriginalObjectId;
        public string ChangedObjectId;
        public int ModifiedCount;
        public ObjectModifyProperty[] ObjectModifyProperties;
    }

    public class ObjectModifyTable
    {
        public ObjectModifyTable(WCLoader Loader, bool UseOptionalInts)
        {
            Count = Loader.ReadInt32();
            ObjectModifyUnits = new ObjectModifyUnit[Count];
            for (int i = 0; i < Count; i++)
                ObjectModifyUnits[i] = new ObjectModifyUnit(Loader, UseOptionalInts);
        }
        public int Count;
        public ObjectModifyUnit[] ObjectModifyUnits;
    }

    public class ObjectModifyFile
    {
        public ObjectModifyFile(WCLoader Loader, bool UseOptionalInts)
        {
            Version = Loader.ReadInt32();
            OriginalTable = new ObjectModifyTable(Loader, UseOptionalInts);
            ChangedTable = new ObjectModifyTable(Loader, UseOptionalInts);
        }

        public int Version;
        public ObjectModifyTable OriginalTable;
        public ObjectModifyTable ChangedTable;
    }
}
